clear all
set more off
set scheme aspen, permanently


********************************
********************************
****-- Replication .do file for Main paper 
********************************
********************************

*-------- Fig. 1 -------

global pathData = ".../rep_data" /* Add the path to your data folder */
global pathFig = ".../rep_fig" /* Add the path to your figures folder */



cd "$pathData"


use "ESS_selected_waves", replace 

keep if year == 2016 

** X-axis, anti-immigrant sentiment
recode imbgeco imueclt imwbcnt  ( 77 88 99 = .)
recode  imdfetn impcntr imsmetn ( 7 8 9 = .)
alpha imbgeco imueclt imwbcnt imdfetn impcntr imsmetn, gen(immi) 

** Y-axis, first redistribution, then support for unemployed 
recode gincdif (2 = 1) (3 4 5 = 0) ( 7 8 9  = .), gen(pro_red) 
recode gvslvue (0/5 = 0) (6/10 = 1) ( 77 88 99 = .), gen(pro_ue) 

local cntryFR "France" 
local cntryDE "Germany" 
local cntryGB "Great Britain" 
local cntrySE "Sweden" 



cd "$pathFig"

foreach var in DE SE FR GB {
preserve
keep if cntry == "`var'"
xtile immi_`var' = immi [pw = pspw], n(10)
collapse pro_ue pro_red [pw = pspw], by(immi_`var')
drop if pro_ue == . | pro_red == . | immi_`var' == . 
tw (scatter pro_ue immi_`var', xscale(range(1(1)10)) xlabel(1(1)10, angle(45)labsize(vsmall)) ///
yscale(range(0.4(0.1)0.8)) ylabel(0.4(0.1)0.8)  xtitle("") ///
ytitle("Gov Should Ensure Liv. Standard of Unemp", size(small)))
graph save "pro_ue_`var'.gph", replace
tw (scatter pro_red immi_`var', xscale(range(1(1)10)) xlabel(1(1)10, angle(45)labsize(vsmall)) ///
 yscale(range(0.4(0.1)0.8)) ylabel(0.4(0.1)0.8)  xtitle("") ///
ytitle("Gov Should Reduce Income Diff.", size(small)))
graph save "pro_red_`var'.gph", replace
graph combine "pro_ue_`var'.gph" "pro_red_`var'.gph", title("`cntry`var''", size(medsmall)) b1title("Anti-Immigrant Sentiment Binned (Positive to Negative)", size(small))
graph save "immi_`var'.gph", replace
restore
}

graph combine "immi_DE.gph" "immi_SE.gph" "immi_FR.gph" "immi_GB.gph", col(2)
graph save "Fig1.gph", replace

*-------- Table. 1 -------

recode imsclbn ( 7 8 9 = .)

matrix EV = J(5,9,0)
matrix colnames EV = "BE" "GB" "DE" "FI" "AT" "FR" "PT" "SE" "NL"
matrix rownames EV = "Immediately" ///
"After a year" ///
"After worked" ///
"Once citizen " ///
"Never" 

local i= 1
foreach cnt in BE GB DE FI AT FR PT SE NL {
di "`cnt'"
prop imsclbn [pw = pspw] if cntry == "`cnt'"
matrix EV[1,`i'] = e(b)[1,1] 
matrix EV[2,`i'] = e(b)[1,2] 
matrix EV[3,`i'] = e(b)[1,3] 
matrix EV[4,`i'] = e(b)[1,4] 
matrix EV[5,`i'] = e(b)[1,5] 
local i = `i' + 1
}


esttab  matrix(EV, fmt("2 2")) using tab1.tex, replace 


